
package cn.org.timespace.ons.util.pager;

import javax.servlet.http.HttpServletRequest;

public class PagerUtil {

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum, String paramOneName, String paramOneValue) {
		pager(request, actionMethod, itemsNum, 10, 10, paramOneName,
				paramOneValue, null, null);
	}

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum, int maxPageItems, String paramOneName,
			String paramOneValue) {
		pager(request, actionMethod, itemsNum, maxPageItems, 10, paramOneName,
				paramOneValue, null, null);
	}

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum, int maxPageItems) {
		pager(request, actionMethod, itemsNum, maxPageItems, 10, null, null,
				null, null);
	}

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum) {
		pager(request, actionMethod, itemsNum, 10, 10, null, null, null, null);
	}

	public static void pager(HttpServletRequest request, int itemsNum,
			String paramOneName, String paramOneValue) {
		pager(request, "list", itemsNum, 10, 10, paramOneName, paramOneValue,
				null, null);
	}
	/**
	 * maxPageItems条记录
	 * @param request
	 * @param itemsNum
	 * @param paramOneName
	 * @param paramOneValue
	 * @param maxPageItems
	 */
	public static void pager(HttpServletRequest request, int itemsNum,
			String paramOneName, String paramOneValue,int maxPageItems) {
		pager(request, "list", itemsNum, maxPageItems, 10, paramOneName, paramOneValue,
				null, null);
	}

	public static void pager(HttpServletRequest request, int itemsNum,
			String paramOneName, String paramOneValue, String paramTwoName,
			String paramTwoValue) {
		pager(request, "list", itemsNum, 10, 10, paramOneName, paramOneValue,
				paramTwoName, paramTwoValue);
	}

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum, String paramOneName, String paramOneValue,
			String paramTwoName, String paramTwoValue) 
	{
		pager(request, actionMethod, itemsNum, 10, 10, paramOneName,
				paramOneValue, paramTwoName, paramTwoValue);
	}

	public static void pager(HttpServletRequest request, int itemsNum,
			int maxPageItems, int maxIndexPages) {
		pager(request, "list", itemsNum, 10, 10, null, null, null, null);
	}

	public static void pager(HttpServletRequest request, int itemsNum) {
		pager(request, itemsNum, 10, 10);
	}

	public static void pager(HttpServletRequest request, String actionMethod,
			int itemsNum, int maxPageItems, int maxIndexPages,
			String paramOneName, String paramOneValue, String paramTwoName,
			String paramTwoValue) {

		String action = request.getRequestURI();

		PagerTagBean pagerTag = new PagerTagBean();

		pagerTag.setForwardAction(action);
		pagerTag.setItemsNum(itemsNum);
		pagerTag.setParamMethod(actionMethod);

		pagerTag.setMaxPageItems(maxPageItems);
		pagerTag.setMaxIndexPages(maxIndexPages);

		pagerTag.setParamOneName(paramOneName);
		pagerTag.setParamOneValue(paramOneValue);
		pagerTag.setParamTwoName(paramTwoName);
		pagerTag.setParamTwoValue(paramTwoValue);

		int totalPages = pagerTag.getItemsNum() / pagerTag.getMaxPageItems();

		if (totalPages * pagerTag.getMaxPageItems() < pagerTag.getItemsNum())
			totalPages++;

		pagerTag.setTotalPages(totalPages);

		String sOffset = request.getParameter("pager.offset");
		int offset = 0;
		if (sOffset != null) {
			offset = Integer.parseInt(sOffset);
		} 
		int currentPage = 0;
		if (offset == 0) {
			currentPage = 1;
		} else {
			currentPage = offset / pagerTag.getMaxPageItems() + 1;
		}
		pagerTag.setCurrentPage(currentPage);

		pagerTag.setOffsetBegin(offset);
		pagerTag.setOffsetEnd(offset + pagerTag.getMaxPageItems() - 1);
		request.setAttribute("pagerTag", pagerTag);
	}


}
